// Angular Version 6 below need to be deleted {providedIn: 'root'}

// angular
import {Injectable} from '@angular/core';
import {HttpParams} from '@angular/common/http';
// owner
import {Request} from 'pengesoft-ng-lib';
// models
import {Device, DeviceList} from '../domains/device.domain';

/**
 *
 */
@Injectable({
  providedIn: 'root'
})
export class DeviceServiceSvr {
  private readonly baseUrl: string;

  constructor(
    private request: Request
  ) {
    this.baseUrl = 'deviceServiceSvr.assx/';
  }


  updateDeviceState(token: string, deviceId: string): Promise<number> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('deviceId', deviceId.toString());
    return this.request.post<number>(this.baseUrl + 'updateDeviceState', httpParams).then((res) => {
      return res;
    });
  }


  updateDevice(token: string, id: string, name: string, houseId: string): Promise<number> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('id', id.toString())
      .append('name', name.toString())
      .append('houseId', houseId.toString());
    return this.request.post<number>(this.baseUrl + 'updateDevice', httpParams).then((res) => {
      return res;
    });
  }


  GetVersion(): Promise<string> {
    const httpParams = new HttpParams();
    return this.request.post<string>(this.baseUrl + 'GetVersion', httpParams).then((res) => {
      return res;
    });
  }


  saveDevice(token: string, name: string, houseId: string): Promise<Device> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('name', name.toString())
      .append('houseId', houseId.toString());
    return this.request.post<Device>(this.baseUrl + 'saveDevice', httpParams).then((res) => {
      return new Device(res);
    });
  }


  deleteDeviceByPrimaryKey(token: string, id: string): Promise<number> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('id', id.toString());
    return this.request.post<number>(this.baseUrl + 'deleteDeviceByPrimaryKey', httpParams).then((res) => {
      return res;
    });
  }


  listDevices(token: string, houseId: string, state: number, skip: number, size: number): Promise<DeviceList> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('houseId', houseId.toString())
      .append('state', state.toString())
      .append('skip', skip.toString())
      .append('size', size.toString());
    return this.request.post<DeviceList>(this.baseUrl + 'listDevices', httpParams).then((res) => {
      // return new DeviceList(res);
      return res;
    });
  }
}
